package com.generator.code.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.net.URI;

@Slf4j
@Component
public class AutoOpenBrowseConfig implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        try {
            String url = "http://localhost:9876";
            // 检测是否支持 Desktop 功能（避免 HeadlessException）
            if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                Desktop.getDesktop().browse(new URI(url));
            } else {
                // 降级方案：调用命令行
                Runtime.getRuntime().exec("cmd /c start " + url);
            }
        } catch (Exception e) {
            log.error("GeneratorApplication open browse has exception : ", e);
        }
    }
}
